﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace Infrastructure.Aspect.Validation
{
    [AttributeUsage(AttributeTargets.Field |  AttributeTargets.Property )]
    public class ValidationNotContainStrings : ValidationAttribute
    {
        private const string _defaultErrorMessage = "{0} not contains these elements {1}";
        public ValidationNotContainStrings()
            : base(_defaultErrorMessage)
        { }

        public string BadWords { get; set; }


        public override string FormatErrorMessage(string name)
        {
            return string.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, this.BadWords);
        }
        public override bool IsValid(object value)
        {
            if (value == null  || string.IsNullOrWhiteSpace(value.ToString())) return true; 
            var words = this.BadWords.Split(',');
            return !words.Contains(value.ToString());
        }
    }
}
